﻿using System;
using System.Timers;
using System.Windows;
using System.Windows.Input;
using System.Windows.Markup;
using SnakeGame.Models;
using SnakeGame.Views;

namespace SnakeGame.ViewModels
{
    public class PlayerPageViewModel : NotifyPropertyChanged
    {
        public ICommand SelectCommand { get; set; }
        public ICommand CancelCommand { get; set; }

        private MainFrame _mainFrame;
        public event EventHandler RequestClose;
        public string PlayerName { get; set; }
        private string ID;

        public bool IsSelected { get; set; }

        public PlayerPageViewModel()
        {
            SelectCommand = new RelayCommand(OnSelectCommand);
            CancelCommand = new RelayCommand(OnCancelCommand);
            IsSelected = false;
        }


        public void Initialize(MainFrame mf, string id)
        {
            _mainFrame = mf;
            ID = id;
            PlayerName = "Player " + _mainFrame.NumberOfPlayer.ToString();

        }

        private void OnCancelCommand(object obj)
        {
            IsSelected = false;
            OnRequestClose();
        }

        private void OnSelectCommand(object obj)
        {
            var p = new Player
            {
                Index = _mainFrame.NumberOfPlayer - 1,
                PlayerName = PlayerName.Trim(),
                CharacterPicture = @"pack://application:,,,/Resources/" + ID + ".png"
            };
            _mainFrame.Players.Add(p);
            _mainFrame.NumberOfPlayer++;
            IsSelected = true;
            OnRequestClose();
        }

        protected void OnRequestClose()
        {
            if (RequestClose != null)
            {
                RequestClose(this, EventArgs.Empty);
            }
        }
    }
}
